package com.curry.apiprovider.crawler.task;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.curry.apiprovider.generator.mapper.TQuartzTagInfoMapper;
import com.curry.apiprovider.generator.service.impl.TQuartzTagInfoServiceImpl;
import com.curry.apiprovider.generator.service.impl.TQuartzTagServiceImpl;
import com.curry.apiprovider.service.QuartzOperatorService;
import com.curry.livehelper.entity.domain.TQuartzInfo;
import com.curry.livehelper.entity.domain.TQuartzTag;
import com.curry.livehelper.entity.domain.TQuartzTagInfo;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

/**
 * 定时器 前尘无忧任务
 */
@DisallowConcurrentExecution
@PersistJobDataAfterExecution
public class ScheduledJob implements Job {


    private Logger logger = LoggerFactory.getLogger(ScheduledJob.class);
    @Autowired
    private TQuartzTagServiceImpl tQuartzTagService;
    @Autowired
    private TQuartzTagInfoServiceImpl tQuartzTagInfoService;
    @Autowired
    private TQuartzTagInfoMapper tQuartzTagInfoMapper;
    @Autowired
    private QuartzOperatorService quartzOperatorService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        logger.warn("ScheduledJob ");
        JobDetail jobDetail = jobExecutionContext.getJobDetail();
        JobDataMap jobDataMap = jobDetail.getJobDataMap();
        TQuartzInfo tQuartzInfo = (TQuartzInfo) jobDataMap.get("TQuartzInfo");
        logger.warn("当前执行的定时器信息 " + tQuartzInfo.toString());
        if (tQuartzInfo != null) {
            String taskTag = tQuartzInfo.getTaskTag();
            String groupTag = tQuartzInfo.getGroupTag();
            EntityWrapper<TQuartzTag> entityWrapper = new EntityWrapper<>(new TQuartzTag());
            entityWrapper.where("taskTag={0}", taskTag).and("groupTag={0}", groupTag);
            TQuartzTag tQuartzTag = tQuartzTagService.selectOne(entityWrapper);
            if (tQuartzTag != null) {
                Integer id = tQuartzTag.getId();
                TQuartzTagInfo tQuartzTagInfo = tQuartzTagInfoMapper.queryByTagId(id);
                if (tQuartzTagInfo != null) {
                    logger.warn("taskId " + tQuartzTagInfo.getTagId() + " class " + tQuartzTagInfo.getClassName() + " method " + tQuartzTagInfo.getMethod());
                    String className = tQuartzTagInfo.getClassName();
                    //查询任务列表获取方法执行的参数
                    String params = tQuartzTagInfo.getParams();
                    JSONObject jsonObject = JSONObject.parseObject(params);
                    String cityName = jsonObject.getString("cityName");
                    String keyWord = jsonObject.getString("keyWord");
                    Integer channelId = jsonObject.getInteger("channelId");
                    try {
                        Class<?> clazz = Class.forName(className);
                        Map<String, Object> taskParam = new HashMap<>();
                        taskParam.put("keyWord", keyWord);
                        taskParam.put("cityName", cityName);
                        taskParam.put("channelId", channelId);
                        quartzOperatorService.startTask(clazz, taskParam);
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    } catch (NoSuchMethodException e) {
                        e.printStackTrace();
                    } catch (InstantiationException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }


}
